﻿Imports System.Windows.Forms
Imports Oracle.DataAccess.Client
Public Class cGeneral
    Public Shared Sub ActivaControles(ByVal Contenedor As Object, ByVal Activa As Boolean)
        For Each ctl As Control In Contenedor
            If TypeOf ctl Is Control Then
                If Not TypeOf ctl Is ToolStrip Then _
                    ctl.Enabled = Activa
            Else
                ActivaControles(ctl, Activa)
            End If
        Next
    End Sub
    Public Shared Sub MarcaDocumento(ByVal Impreso As Boolean, ByVal Cerrado As Boolean, ByVal TipoDoc As String, ByVal Numero As Integer)
        Using conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
            Using cmd As OracleCommand = conn.CreateCommand
                cmd.CommandText = "UPDATE MOV_MOVIMIENTO SET IMPRESO=:PIMPRESO, CERRADO=:PCERRADO WHERE IDDOCUMENTO=:PIDDOCUMENTO AND NUMERO=:PNUMERO "
                cmd.Parameters.Add("PIMPRESO", OracleDbType.Char).Value = IIf(Impreso, "S", "N")
                cmd.Parameters.Add("PCERRADO", OracleDbType.Char).Value = IIf(Cerrado, "S", "N")
                cmd.Parameters.Add("PIDDOCUMENTO", OracleDbType.Varchar2).Value = TipoDoc
                cmd.Parameters.Add("PNUMERO", OracleDbType.Int64).Value = Numero
                Try
                    conn.Open()
                    cmd.ExecuteNonQuery()
                    conn.Close()
                Catch ex As Exception
                    Throw ex
                End Try
            End Using
        End Using
    End Sub

    Public Shared Function GetConsecGastos() As Integer
        Using conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
            Using cmd As OracleCommand = conn.CreateCommand
                cmd.CommandText = "SELECT MAX(NUMERO) FROM CAJ_GASTO"
                Try
                    conn.Open()
                    Dim NConsec As Integer = cGeneral.NZ(cmd.ExecuteScalar)
                    conn.Close()
                    Return NConsec + 1
                Catch ex As Exception
                    Throw ex
                End Try
            End Using

        End Using
    End Function

    Public Shared Function GetTotalAbonos(ByVal documento As String, ByVal numero As Decimal) As Decimal
        Using conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
            Using cmd As OracleCommand = conn.CreateCommand
                cmd.CommandText = "SELECT CAR_CALCULOABONOSRECIBOS(:PDOC,:PNUM)  FROM DUAL"
                Try
                    cmd.Parameters.Add("PDOC", OracleDbType.Varchar2).Value = documento
                    cmd.Parameters.Add("PNUM", OracleDbType.Decimal).Value = numero
                    conn.Open()
                    Dim TotalAbonos As Decimal = cGeneral.NZ(cmd.ExecuteScalar)
                    conn.Close()
                    Return TotalAbonos
                Catch ex As Exception
                    Throw ex
                End Try
            End Using
        End Using
    End Function

    Public Shared Sub bloquearDocumento(ByVal Contenedor As Object, ByVal Activa As Boolean)
        For Each ctl As Control In Contenedor
            If TypeOf ctl Is SplitterPanel Or TypeOf ctl Is TabControl Or TypeOf ctl Is SplitContainer Or TypeOf ctl Is Panel Then _
                 bloquearDocumento(ctl.Controls, Activa)
            If TypeOf ctl Is TextBox Then _
                    ctl.Enabled = Activa
            If TypeOf ctl Is Button Then _
                ctl.Enabled = Activa
            If TypeOf ctl Is CheckBox Then _
                ctl.Enabled = Activa
            If TypeOf ctl Is ComboBox Then _
               ctl.Enabled = Activa
            If TypeOf ctl Is DataGridView Then _
                ctl.Enabled = Activa
            If TypeOf ctl Is GroupBox Then _
                ctl.Enabled = Activa
            If TypeOf ctl Is DateTimePicker Then _
                ctl.Enabled = Activa
            'If TypeOf ctl Is Panel Then _
            '    ctl.Enabled = Activa
        Next

    End Sub

    Public Shared Function GetMenor(ByVal Num1 As Integer, ByVal Num2 As Integer) As Integer
        If Num1 > Num2 Then
            Return Num2
        Else
            Return Num1
        End If
    End Function
    Public Shared Sub InsertaCadConn(ByVal CadConn As String)
        Try
            My.Settings("ConnectionString") = CadConn
        Catch ex As Exception
            cError.SalvaError(ex, "Samuel")
            Throw ex
        End Try
    End Sub

    Public Shared Function DocEs(ByVal pDocEs As Int16) As String
        Try
            Dim daDatosTablas As New lbGlobalcom.dsGlobalTableAdapters.GLO_DATOSTABLASTableAdapter
            Dim SubClase As String = daDatosTablas.GetSubclase(pDocEs, "td")
            Return SubClase
        Catch ex As Exception
            cError.SalvaError(ex, "Samuel")
            Throw ex
        End Try
    End Function

    Public Shared Function NToCad(ByVal Valor As Object) As String
        If Valor Is Nothing Or Valor Is DBNull.Value Then
            Return ""
        Else
            Return Valor
        End If
    End Function

    Public Shared Function NZ(ByVal Valor As Object) As Object
        If Valor Is Nothing Or Valor Is DBNull.Value Or Valor.ToString.Trim.Length = 0 Then
            Return 0
        Else
            Return Valor
        End If
    End Function

    Public Shared Function GetNombreItemDatosTabla(ByVal Id As Byte, ByVal IdGrupo As String) As String
        Try
            Using conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
                Using cmd As OracleCommand = conn.CreateCommand
                    cmd.CommandText = "SELECT  DESCRIPCION " & _
                                        "FROM GLO_DATOSTABLAS " & _
                                        "WHERE  (IDGRUPO = :PIDGRUPO) AND (ID = :PID)"
                    cmd.Parameters.Add("PIDGRUPO", OracleDbType.Varchar2).Value = IdGrupo
                    cmd.Parameters.Add("PID", OracleDbType.Int32).Value = Id
                    Try
                        conn.Open()
                        Dim Nombre As String = cmd.ExecuteScalar
                        conn.Close()
                        Return Nombre
                    Catch ex As Exception
                        Throw ex
                    End Try
                End Using
            End Using
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Shared Sub EliminaPrepackInv(ByVal drMov As dsMovimiento.MOV_MOVIMIENTORow)
        Try
            Dim pareja As String = drMov.IDDOCUMENTO + drMov.NUMERO.ToString
            Using conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
                Using cmd As OracleCommand = conn.CreateCommand
                    cmd.CommandText = "DELETE FROM INV_PREPACK " & _
                                      " WHERE  PAREJA=:PPAREJA"
                    cmd.Parameters.Add("PPAREJA", OracleDbType.Varchar2).Value = pareja
                    Try
                        conn.Open()
                        cmd.ExecuteNonQuery()
                        conn.Close()
                    Catch ex As Exception
                        Throw ex
                    End Try
                End Using
            End Using
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Shared Function cantidadPrepackFacturados(ByVal drMov As dsMovimiento.MOV_MOVIMIENTORow) As Integer
        Try
            Dim pareja As String = drMov.IDDOCUMENTO + drMov.NUMERO.ToString
            Using conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
                Using cmd As OracleCommand = conn.CreateCommand
                    cmd.CommandText = "SELECT COUNT(*) FROM INV_PREPACK " & _
                                      " WHERE  PAREJA=:PPAREJA AND ESTADO='N'"
                    cmd.Parameters.Add("PPAREJA", OracleDbType.Varchar2).Value = pareja
                    Try
                        conn.Open()
                        Return cmd.ExecuteScalar
                    Catch ex As Exception
                        Throw ex
                    Finally
                        conn.Close()
                    End Try
                End Using
            End Using
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Shared Function GetTiposPrepacks() As dsInventarios.INV_TIPOSPREPACKSDataTable
        Try
            Dim daTipoPPack As New lbGlobalcom.dsInventariosTableAdapters.INV_TIPOSPREPACKSTableAdapter
            Dim dtTPPack As dsInventarios.INV_TIPOSPREPACKSDataTable = daTipoPPack.GetData()
            'Dim drTipoPPack As dsInventarios.INV_TIPOSPREPACKSRow = dtTPPack.NewINV_TIPOSPREPACKSRow
            'drTipoPPack.IDPREPACK = 0
            'drTipoPPack.DESCRIPCION = "No Prepacks"
            'drTipoPPack.CANTICCID = 0
            'drTipoPPack.CANTIMEI = 0
            'dtTPPack.Rows.Add(drTipoPPack)
            Return dtTPPack
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Shared Sub BloquearOrdenDatagrid(ByVal dg As DataGridView)
        For i As Integer = 0 To dg.Columns.Count - 1
            dg.Columns(i).SortMode = DataGridViewColumnSortMode.NotSortable
        Next
    End Sub
    Public Shared Function GetNumeroLinea(ByVal dg As DataGridView, ByVal Fila As Integer, Optional ByVal NomColum As String = "LINEA") As Integer
        If Fila = 0 Then
            Return 1
        Else
            Return dg.Rows(Fila - 1).Cells(NomColum).Value + 1
        End If
    End Function


End Class
